<?php
/*
*  Created:  2011-04-09
*  团购数据组件
*  @Author nic <nic-chen@qq.com>
*/
class grouponCom {

	private $table = 'nic_groupon';
  /*
   * 获取团购数量
   * @return int
   */
	function getGrouponNum() {
		$db = APP :: ADP('db');
		$select_count = 'SELECT COUNT(*) FROM ' . $this->table . '';
		return RST($db->getOne($select_count));
	}

	/*
     * 根据name获取团购数据
     * @param int $name
     * @param int $offset
     * @param int $each
     * @return array()
     */
	function getGrouponByName($name = '', $offset = 0, $each = 1) {
		$db = APP :: ADP('db');
		$keyword = $db->escape($name);
		$where = '';
		if ($keyword) {
				$where = ' WHERE `name` = \'' . $keyword .'\'';
		}

		$sql = 'SELECT * FROM ' . $this->table . $where . ' ORDER BY `id` desc LIMIT ' . $offset . ',' . $each;

		return RST($db->query($sql));
	}

	/*
     * 获取今日团购列表
     * @param int $offset
     * @param int $each
     * @return array()
     */
	function getGrouponTodayList($offset = 0, $each = 4)
	{
		$db = APP :: ADP('db');

		$where = ' WHERE `end` > \'' . date('Y-m-d H:i:s',APP_LOCAL_TIMESTAMP) .'\'';

		$sql = 'SELECT * FROM ' . $this->table . $where . ' ORDER BY `id` desc LIMIT ' . $offset . ',' . $each;

		return RST($db->query($sql));
	}

	/*
     * 获取往期团购列表
     * @param int $name
     * @param int $offset
     * @param int $each
     * @return array()
     */
	function getPastList($offset = 0, $each = 10)
	{
		$db = APP :: ADP('db');

		$where = ' WHERE `start` > \'' . date('Y-m-d H:i:s',APP_LOCAL_TIMESTAMP) .'\'';

		$sql = 'SELECT * FROM ' . $this->table . $where . ' ORDER BY `id` desc LIMIT ' . $offset . ',' . $each;

		return RST($db->query($sql));
	}

    /*
     * 团购删除
     * @param int $id
     * @return boolean
     */
	function delGroupon($id) {
		if (!is_numeric($id)) {
			return RST(false, $errno=1210002, $err='Parameter must be a number');
		}
		$this->_cleanCache();
		$db = APP :: ADP('db');
		$db->setTable($this->table);
		return RST($db->delete($id));

	}

    /*
     * 根据id获取团购信息
     * @param int $id
     * @return array()
     */
	function getGrouponById($id) {
		if (!is_numeric($id)) {
			return RST(false, $errno=1210002, $err='Parameter must be a number');
		}

		$db = APP :: ADP('db');
	
		$sql = 'SELECT * FROM ' . $this->table . ' WHERE id="' . $id . '"';
		return RST($db->getRow($sql));
	
	}

    /*
     * 修改,插入团购数据
     * @param array $data
     * @param int $id
     * @return boolean
     */
	function saveGrouponById($data, $id = '') {
		if(!is_array($data)) {
             return RST(false, $errno=1210000, $err='Parameter can not be empty');
        }
		$this->_cleanCache();
		$db = APP :: ADP('db');
		$db->setTable($this->table);
        return RST($db->save($data, $id));
	}

	/*
	 * 清除缓存
	 */
	function _cleanCache() {
		DD('mgr/adminCom.getGrouponNum');
		DD('mgr/adminCom.getGrouponById');
		DD('mgr/adminCom.getGrouponByName');
	}
}
